#include <climits>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

vector<int> smallestDifference(vector<int> array1, vector<int> array2) {
  sort(array1.begin(), array1.end());
  sort(array2.begin(), array2.end());

  int idx1 = 0; // array1 的索引
  int idx2 = 0; // array2 的索引

  int current = INT_MAX;
  int smallest = INT_MAX;
  vector<int> smallestPair; // 保存计算出 smallest 值的两个数字

  // 只要 idx1 和 idx2 都不出界
  while (idx1 < array1.size() && idx2 < array2.size()) {
    int firstNum = array1[idx1];
    int secondNum = array2[idx2];

    if (firstNum < secondNum) {
      current = secondNum - firstNum;
      idx1 += 1;
    }
    else if (firstNum > secondNum) {
      current = firstNum - secondNum;
      idx2 += 1;
    }
    else {
      return {firstNum, secondNum};
    }

    if (current < smallest) {
      smallest = current;
      smallestPair = {firstNum, secondNum};
    }
  }

  return smallestPair;
}


int main(int argc, char const *argv[])
{
  /* code */
  vector<int> array1 {2, 3, 4};
  vector<int> array2 {5, 18, 20};

  vector<int> result = smallestDifference(array1, array2);

  for (int n : result) {
    cout<<n<<" ";
  }

  cout<<endl;
  return 0;
}

